<?php
namespace app\Common;


function formatDate($time = 0){
    $tmp = time() - $time;
    if($tmp && $tmp <= 60 * 60){
        return ceil($tmp / 60). '分钟前';
    }elseif($tmp > 60 * 60 && $tmp < 60 * 60 * 24){
        return ceil($tmp / 60 / 60)  . '小时前';
    }elseif($tmp > 60 * 60 * 24 && $tmp <= 60 * 60 * 24 * 7){
        return floor($tmp / 60 / 60 / 24) . '天前';
    }else{
        return date('Y年n月j日 H:i',$time);
    }
}
function formatDate2($time = 0){
    $today = strtotime(date('Y-m-d',time()));
    $tmp = $today - $time;
    if($tmp && $tmp <=0){
        return date('H:i',$time);
    }elseif($tmp > 0 && $tmp <= 60 * 60 * 24){
        return '昨天 '.date('H:i',$time);
    }elseif($tmp > 60 * 60 * 24 && $tmp <= 60 * 60 * 24 * 7){
        return getWeek($time).' '.date('H:i',$time);
    }else{
        return date('Y年n月j日 H:i', $time);
    }
}
function formatDate3($time = 0){
    if($time!=0){
        $today = strtotime(date('Y-m-d',time()));
        $tmp = $today - $time;
        if($tmp && $tmp <=0){
            return date('H:i',$time);
        }elseif($tmp > 0 && $tmp <= 60 * 60 * 24){
            return '昨天';
        }elseif($tmp > 60 * 60 * 24 && $tmp <= 60 * 60 * 24 * 7){
            return getWeek($time);
        }else{
            return date('y/n/j', $time);
        }
    }else{
        return '';
    }
}
function getWeek($time){
    $week=array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
    return $week[date("w",$time)];
}
function secToTime($times){
    $result = '00:00:00';
    if ($times>0) {
        $hour = floor($times/3600);
        $minute = floor(($times-3600 * $hour)/60);
        $second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
        $result = $hour.':'.$minute.':'.$second;
    }
    return $result;
}
function diffBetweenTwoDays ($min, $max)
{
    $begin = strtotime(date('Y/m/d',$min));
    $end = strtotime(date('Y/m/d',$max));
    $l = $end - $begin;
    return $l/60/60/24;
}
function getFirstChar($s0){
    if($s0[0]=='I' || $s0[0]=='i'){
        return "I";
    }elseif($s0[0]=='U' || $s0[0]=='u'){
        return 'U';
    }elseif($s0[0]=='V' || $s0[0]=='v'){
        return 'V';
    }else{
        $fchar = ord($s0{0});
        if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
        $s1 = iconv("UTF-8","gb2312", $s0);
        $s2 = iconv("gb2312","UTF-8", $s1);
        if($s2 == $s0){$s = $s1;}else{$s = $s0;}
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if($asc >= -20319 and $asc <= -20284) return "A";
        if($asc >= -20283 and $asc <= -19776) return "B";
        if($asc >= -19775 and $asc <= -19219) return "C";
        if($asc >= -19218 and $asc <= -18711) return "D";
        if($asc >= -18710 and $asc <= -18527) return "E";
        if($asc >= -18526 and $asc <= -18240) return "F";
        if($asc >= -18239 and $asc <= -17923) return "G";
        if($asc >= -17922 and $asc <= -17418) return "H";
        if($asc >= -17417 and $asc <= -16475) return "J";
        if($asc >= -16474 and $asc <= -16213) return "K";
        if($asc >= -16212 and $asc <= -15641) return "L";
        if($asc >= -15640 and $asc <= -15166) return "M";
        if($asc >= -15165 and $asc <= -14923) return "N";
        if($asc >= -14922 and $asc <= -14915) return "O";
        if($asc >= -14914 and $asc <= -14631) return "P";
        if($asc >= -14630 and $asc <= -14150) return "Q";
        if($asc >= -14149 and $asc <= -14091) return "R";
        if($asc >= -14090 and $asc <= -13319) return "S";
        if($asc >= -13318 and $asc <= -12839) return "T";
        if($asc >= -12838 and $asc <= -12557) return "W";
        if($asc >= -12556 and $asc <= -11848) return "X";
        if($asc >= -11847 and $asc <= -11056) return "Y";
        if($asc >= -11055 and $asc <= -10247) return "Z";
        return '#';
    }
}
function headPic(array $pic_list,$name,$basePath=null){
    if($basePath==null){
        if(strpos($name, "images")!==false) $path=public_path('uploads/').$name;
        else $path = public_path('uploads').'/images/'.$name;
    }else{
        if(substr(trim($basePath),-1)=='/') $path=$basePath.'images/'.$name;
        else $path=$basePath.'/images/'.$name;
    }
    $pic_list    = array_slice($pic_list, 0, 9); // 只操作前9个图片
    $bg_w    = 150; // 背景图片宽度
    $bg_h    = 150; // 背景图片高度
    $background = imagecreatetruecolor($bg_w,$bg_h); // 背景图片
    $color   = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景，再设置为透明
    imagefill($background, 0, 0, $color);
    imageColorTransparent($background, $color);
    $pic_count  = count($pic_list);
    $lineArr    = array();  // 需要换行的位置
    $space_x    = 3;
    $space_y    = 3;
    $line_x  = 0;
    switch($pic_count) {
        case 1: // 正中间
            $start_x    = intval($bg_w/4);  // 开始位置X
            $start_y    = intval($bg_h/4);  // 开始位置Y
            $pic_w   = intval($bg_w/2); // 宽度
            $pic_h   = intval($bg_h/2); // 高度
            break;
        case 2: // 中间位置并排
            $start_x    = 2;
            $start_y    = intval($bg_h/4) + 3;
            $pic_w   = intval($bg_w/2) - 5;
            $pic_h   = intval($bg_h/2) - 5;
            $space_x    = 5;
            break;
        case 3:
            $start_x    = 40;   // 开始位置X
            $start_y    = 5;    // 开始位置Y
            $pic_w   = intval($bg_w/2) - 5; // 宽度
            $pic_h   = intval($bg_h/2) - 5; // 高度
            $lineArr    = array(2);
            $line_x  = 4;
            break;
        case 4:
            $start_x    = 4;    // 开始位置X
            $start_y    = 5;    // 开始位置Y
            $pic_w   = intval($bg_w/2) - 5; // 宽度
            $pic_h   = intval($bg_h/2) - 5; // 高度
            $lineArr    = array(3);
            $line_x  = 4;
            break;
        case 5:
            $start_x    = 30;   // 开始位置X
            $start_y    = 30;   // 开始位置Y
            $pic_w   = intval($bg_w/3) - 5; // 宽度
            $pic_h   = intval($bg_h/3) - 5; // 高度
            $lineArr    = array(3);
            $line_x  = 5;
            break;
        case 6:
            $start_x    = 5;    // 开始位置X
            $start_y    = 30;   // 开始位置Y
            $pic_w   = intval($bg_w/3) - 5; // 宽度
            $pic_h   = intval($bg_h/3) - 5; // 高度
            $lineArr    = array(4);
            $line_x  = 5;
            break;
        case 7:
            $start_x    = 53;   // 开始位置X
            $start_y    = 5;    // 开始位置Y
            $pic_w   = intval($bg_w/3) - 5; // 宽度
            $pic_h   = intval($bg_h/3) - 5; // 高度
            $lineArr    = array(2,5);
            $line_x  = 5;
            break;
        case 8:
            $start_x    = 30;   // 开始位置X
            $start_y    = 5;    // 开始位置Y
            $pic_w   = intval($bg_w/3) - 5; // 宽度
            $pic_h   = intval($bg_h/3) - 5; // 高度
            $lineArr    = array(3,6);
            $line_x  = 5;
            break;
        case 9:
            $start_x    = 5;    // 开始位置X
            $start_y    = 5;    // 开始位置Y
            $pic_w   = intval($bg_w/3) - 5; // 宽度
            $pic_h   = intval($bg_h/3) - 5; // 高度
            $lineArr    = array(4,7);
            $line_x  = 5;
            break;
    }
    foreach( $pic_list as $k=>$pic_path ) {
        $kk = $k + 1;
        if ( in_array($kk, $lineArr) ) {
            $start_x    = $line_x;
            $start_y    = $start_y + $pic_h + $space_y;
        }
        $pathInfo    = pathinfo($pic_path);
        switch( strtolower($pathInfo['extension']) ) {
            case 'jpg':
            case 'jpeg':
                $imagecreatefromjpeg    = 'imagecreatefromjpeg';
                break;
            case 'png':
                $imagecreatefromjpeg    = 'imagecreatefrompng';
                break;
            case 'gif':
            default:
                $imagecreatefromjpeg    = 'imagecreatefromstring';
                $pic_path    = file_get_contents($pic_path);
                break;
        }
        $resource   = $imagecreatefromjpeg($pic_path);
        // $start_x,$start_y copy图片在背景中的位置
        // 0,0 被copy图片的位置
        // $pic_w,$pic_h copy后的高度和宽度
        imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource)); // 最后两个参数为原始图片宽度和高度，倒数两个参数为copy时的图片宽度和高度
        $start_x    = $start_x + $pic_w + $space_x;
    }
    imagejpeg($background,$path);
}